package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.qiantu.api.entity.DeviceShareRecord;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.DeviceShareRecordAdapter;
import com.qiantu.phone.ui.dialog.ConfirmHintDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * time   : 2021/12/22
 */
public final class DeviceShareRecordActivity extends AppActivity {

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, DeviceShareRecordActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private RecyclerView mRecyclerView;
    private DeviceShareRecordAdapter mDeviceShareRecordAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_share_record;
    }

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDeviceShareRecordAdapter = new DeviceShareRecordAdapter(this);
        mRecyclerView.setAdapter(mDeviceShareRecordAdapter);
        mDeviceShareRecordAdapter.setOnChildClickListener(R.id.btn_recover, new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                showRecoverDialog(R.string.recover_device_share_hint);
            }
        });
    }

    @Override
    protected void initData() {
        List<DeviceShareRecord> data = new ArrayList<>();
        for(int i=0; i<10; i++){
            data.add(new DeviceShareRecord());
        }
        mDeviceShareRecordAdapter.setData(data);
    }

    private BaseDialog mConfirmHintDialog;
    private ConfirmHintDialog.Builder mConfirmHintDialogBuilder;
    private void showRecoverDialog(int res){
        if(mConfirmHintDialog == null){
            mConfirmHintDialogBuilder = new ConfirmHintDialog.Builder(getActivity());
            mConfirmHintDialogBuilder.setListener(new ConfirmHintDialog.OnListener() {
                @Override
                public void onLeftClick(BaseDialog dialog) {
                    mConfirmHintDialog.dismiss();
                }

                @Override
                public void onConfirm(BaseDialog dialog) {
                    mConfirmHintDialog.dismiss();
                }
            });
            mConfirmHintDialog = mConfirmHintDialogBuilder.setLeftBtnText(getResources().getString(R.string.cancel)).create();
        }
        mConfirmHintDialogBuilder.setContent(res);
        mConfirmHintDialog.show();
    }
}